<?php
namespace app\services;
use think\facade\Env;
// use vendor\PHPExcel\PHPExcel;

class PHPExcelService
{
    public function __construct()
    {

    }

    //读取excel
    public function excelLoadFile($savename)
    {
        //读取
        // vendor('phpExcel.PHPExcel');
        require_once Env::get('vendor_path').'PHPExcel/PHPExcel.php';

        $objPHPExcel  = new \PHPExcel();

        $objRead = new \PHPExcel_Reader_Excel2007($objPHPExcel);

        $PHPExcel = $objRead->load($savename,'utf-8'); // 载入excel文件

        $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表

        $highestRow = $sheet->getHighestRow(); // 取得总行数

        $highestColumm = $sheet->getHighestColumn(); // 取得总列数

        //把Excel数据保存数组中
        $data = array();

        //循环读取每个单元格的内容。注意行从1开始，列从A开始
        for ($rowIndex = 1; $rowIndex <= $highestRow; $rowIndex++)
        {
            //确定从哪一行开始读取
            if($rowIndex <= 2)
            {
                continue;
            }

            for ($colIndex = 'A'; $colIndex <= $highestColumm; $colIndex++)
            {

                $addr = $colIndex . $rowIndex;

                $cell = $sheet->getCell($addr)->getValue();

                if(is_object($cell)){
                    $cell= $cell->__toString();
                }

                $data[$rowIndex][$colIndex] = $cell;
            }
        }

        return $data;
    }


    //读取全部excel
    public function excelallLoadFile($savename)
    {
        //读取
        // vendor('phpExcel.PHPExcel');
        require_once Env::get('vendor_path').'PHPExcel/PHPExcel.php';

        $objPHPExcel  = new \PHPExcel();

        $objRead = new \PHPExcel_Reader_Excel2007($objPHPExcel);

        $PHPExcel = $objRead->load($savename,'utf-8'); // 载入excel文件

        $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表

        $highestRow = $sheet->getHighestRow(); // 取得总行数

        $highestColumm = $sheet->getHighestColumn(); // 取得总列数

        //把Excel数据保存数组中
        $data = array();

        //循环读取每个单元格的内容。注意行从1开始，列从A开始
        for ($rowIndex = 1; $rowIndex <= $highestRow; $rowIndex++)
        {
            for ($colIndex = 'A'; $colIndex <= $highestColumm; $colIndex++)
            {

                $addr = $colIndex . $rowIndex;

                $cell = $sheet->getCell($addr)->getValue();

                if(is_object($cell)){
                    $cell= $cell->__toString();
                }

                $data[$rowIndex][$colIndex] = $cell;
            }
        }

        return $data;
    }

    //读取全部excel
    public function excelall($savename)
    {
        //读取
        // vendor('phpExcel.PHPExcel');
        require_once Env::get('vendor_path').'PHPExcel/PHPExcel.php';
        $objPHPExcel  = new \PHPExcel();

        $objRead = new \PHPExcel_Reader_Excel2007($objPHPExcel);

        //检查文件内容
        // if(!$objRead->canRead($savename)){

        //     $objRead = new PHPExcel_Reader_Excel5();

        //     if(!$objRead->canRead($savename)){

        //         return [];
        //     }
        // }

        $PHPExcel = $objRead->load($savename,'utf-8'); // 载入excel文件

        $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表

        $highestRow = $sheet->getHighestRow(); // 取得总行数

        $highestColumm = $sheet->getHighestColumn(); // 取得总列数

        //把Excel数据保存数组中
        $data = array();

        //循环读取每个单元格的内容。注意行从1开始，列从A开始
        for ($rowIndex = 2; $rowIndex <= $highestRow; $rowIndex++)
        {
            for ($colIndex = 'A'; $colIndex <= $highestColumm; $colIndex++)
            {

                $addr = $colIndex . $rowIndex;

                $cell = $sheet->getCell($addr)->getValue();

                if ($cell instanceof PHPExcel_RichText)
                { //富文本转换字符串
                    $cell = $cell->__toString();
                }

                $data[$rowIndex][$colIndex] = $cell;
            }
        }

        return $data;
    }
}